/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number[][]}
 */
var pathSum = function (root, targetSum) {
    if (root == null) return []
    const ans = []
    let path = []
    const backtracking = (node, targetSum) => {
        path.push(node.val)
        // 如果只有非负数，可以在这里加个判断剪枝
        // if(node.val > targetSum) {
        //     path.pop()
        //     return
        // }
        if(node.left === null && node.right == null && targetSum === node.val) {
            ans.push(JSON.parse(JSON.stringify(path)))
        }
        if (node.left)
            backtracking(node.left, targetSum - node.val)
        if (node.right)
            backtracking(node.right, targetSum - node.val)
        path.pop()
    }
    backtracking(root, targetSum)
    return ans
};